<template>
  <div class="login">
    <div class="login-man">
      <img src="../assets/login-man.png" alt="">
    </div>
    <div class="login-box">
      <div class="box-right">
        <!--头像区-->
        <img src="../assets/loginTip.png" alt="">
        <h3>欢迎使用数字人交互平台</h3>
        <!--登录表单区-->
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" class="login-form">
          <!--用户名-->
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
          </el-form-item>
          <!--密码-->
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" prefix-icon="iconfont icon-cfmpassword" type="password"></el-input>
          </el-form-item>
          <!--按钮-->
          <el-form-item>
            <el-button type="primary" @click="login" class="login-btn">
              <span>登录</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 登录表单的数据绑定对象
      loginForm: {
        username: '',
        password: ''
      },
      // 表单的验证规则
      loginFormRules: {
        // 验证用户是否合法
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login () {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (!valid) return
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status !== 200) this.$message.error('登录失败！')
        else this.$message.success('登录成功！')
        // 将token保存到sessionStorage中
        window.sessionStorage.setItem('token', res.data.token)
        console.log(res)
        // 页面跳转
        await this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>

.login{
  background: linear-gradient(109.31deg,#2622d1 5.75%,#4386f9 35.66%);
  height: 100%;
  display: flex
}

.login .login-man{
  flex: 1;
  position: relative;
  height:100%;
}

.login .login-man img{
  width: 70%;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-30%,-50%)
}

//登录表单
.login .login-box{
  flex: 1;
  position: relative;
}

.login-box .box-right {
  width: 380px;
  text-align: center;
  background-color: white;
  border-radius: 10px;
  padding: 80px 30px 20px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-60%,-50%);
}

.login .box-right img {
  width: 180px;
  position: absolute;
  top: 6px;
  left: 50%;
  transform: translate(-50%);
  z-index: 2
}

.login .box-right h3 {
  position: relative;
  z-index: 3
}

.login .login-form{
  position: relative;
  min-width: 50%;
  padding: 10px 25px 5px;
}

//登录框按钮
.login-btn{
  background: linear-gradient(93.11deg,#6076fc 3.08%,#8134ff 103.7%);
  width: 100%;
}

</style>
